<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <button>1</button>
    <button>2</button>
    <button>3</button>

    <script>

        var oBtns = document.querySelectorAll('button') ;

        for(var i = 0 ; i < oBtns.length ; i++) {

            // 
            // oBtns[i].index = i ;   // 给js对象添加自定义属性   --- 看不见

            oBtns[i].setAttribute('index' , i) ;   // 给标签添加自定义属性    --- 看得见


            oBtns[i].onclick = function() {
                // console.log(i);   // 3
                // console.log(this.index);

                console.log(this.getAttribute('index'));
            }
        }

    </script>
    
</body>
</html>